<?php
require 'Database.class.php';
require 'smarty/Smarty.class.php';
require 'Model.class.php';

class Controller extends Smarty
{
	//static public $db = null;
	static public $tpl = null;
	
	function __construct()
	{
		self::$tpl = new Smarty();
		self::$tpl->compile_check = true;
		self::$tpl->debugging = false;
		self::$tpl->template_dir = DOCROOT.'/tpl/';
		self::$tpl->compile_dir  = DOCROOT.'/tpl_compiled/';
		self::$tpl->force_compile = 1;
		
		
		//self::$db = new Database(config_item('database'));
	}
	
	public function render()
	{
		$module = ($_GET['module']) ? $_GET['module'] : config_item('module');
		$method = ($_GET['method']) ? $_GET['method'] : 'index';
		if ($method == 'index' && $_GET['method'] != 'index')
		{
			header('Location: /admin/'.$module.'/'.$method.'.html');
			exit();
		}
		
		/* URLs like site.com/login controlled by common (lib.common.class.php) controller */
		
		self::$tpl->assign('CURR_PAGE', $module.'/'.$method);
		
		//set_error_handler(create_function('', "throw new Exception();"));
		try
		{
			$OBJ = load_class($module);
			if (method_exists($OBJ, $method))
			{
				$OBJ->$method();
				self::$tpl->display('skin.tpl');
			} else throw new Exception('');
		} catch (Exception $e){ echo 'routing error';exit(); }
		
	}
	
	
	
	public function _check_login($key)
	{
		if ((int)$_SESSION[$key])
			return true;
		return false;
	}
	
	
	function __destruct(){}
}
?>